package cn.virens.service.impl.sys.user;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.virens.CacheKeys;
import cn.virens.common.MapUtil;
import cn.virens.database.common.TransactionExecute;
import cn.virens.database.common.mapper.ExampleWrapper;
import cn.virens.database.mysql.mapper.sys.user.SysUserRoleResourceMapper;
import cn.virens.database.mysql.model.sys.user.SysUserRoleResource;
import cn.virens.service.inter.sys.user.SysUserRoleResourceService;

/**
 * 用户表-角色-资源关系 - sys_user_role_resource
 *
 * @author Virens
 * @since  2019-10-25
 */
@Service
public class SysUserRoleResourceServiceImpl implements SysUserRoleResourceService {
	private @Autowired SysUserRoleResourceMapper mSysUserRoleResourceMapper;
	private @Autowired TransactionExecute mTransactionExecute;

	@Override
	@CacheEvict(cacheNames = CacheKeys.CACHE_MENUS, allEntries = true)
	public int putResource(String identity, JSONArray array) {
		return mTransactionExecute.write((s) -> {
			// 删除已有的资源配置-----------------------------------------------------------
			ExampleWrapper example = ExampleWrapper.of(SysUserRoleResource.class);
			example.and().andEqualTo("identity", identity);

			int result = mSysUserRoleResourceMapper.deleteByExample(example);

			// 添加新配置的资源内容----------------------------------------------------------
			SysUserRoleResource record = new SysUserRoleResource();
			record.setIdentity(identity);

			for (int i = 0; i < array.size(); i++) {
				JSONObject obj = array.getJSONObject(i);
				Boolean view = obj.getBooleanValue("view");
				Boolean manage = obj.getBooleanValue("manage");

				if (view && manage) {
					record.setResource(obj.getString("identity"));
					record.setPermission("view,manage");
				} else if (!view && manage) {
					record.setResource(obj.getString("identity"));
					record.setPermission("manage");
				} else if (view && !manage) {
					record.setResource(obj.getString("identity"));
					record.setPermission("view");
				} else {
					continue;
				}

				result += mSysUserRoleResourceMapper.insert(record);
			}

			return result;
		});
	}

	@Override
	public List<Map<String, Object>> getResource(String identity) {
		List<Map<String, Object>> list = mSysUserRoleResourceMapper.selectResource(identity);

		return list.stream().filter((m) -> pid(m) == 0).map((item) -> {
			item.put("childrens", list.stream().filter((children) -> {
				return pid(children) == id(item);
			}).collect(Collectors.toList()));

			return item;
		}).collect(Collectors.toList());
	}

	private static int id(Map<String, Object> map) {
		return MapUtil.getIntValue(map, "id");
	}

	private static int pid(Map<String, Object> map) {
		return MapUtil.getIntValue(map, "pid");
	}
}
